package app.blog.util;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import jodd.util.StringUtil;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import slap.Resources;
import slap.Slap;
import slap.berkeleydb.BDB;
import slap.util.Xml;

import app.blog.entity.Category;
import app.blog.entity.Option;
import app.blog.entity.Page;
import app.blog.entity.Article;
import app.blog.entity.Tag;
import app.blog.entity.User;


public class Importer {
	
	public static void main(String[] args){
		try {
			fromWp(new File(Resources.Presets.DIR_HOME+"tmp/wp-export.xml"));
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
	}
	
	public static void fromWp(File xmlFile) throws DocumentException, ParseException{
		
		Document document = Xml.openXml(xmlFile);
		
		parse_option(document);
		parse_tag(document);
		parse_category(document);
		parse_post(document);
		
	}
	
	public static void parse_tag(Document document){
		for(Node item : document.selectNodes("//rss/channel/wp:tag")){
			Tag tag = new Tag();
			tag.name = Xml.getText(item, "wp:tag_name");
			tag.slug = Xml.getText(item, "wp:tag_slug");
			
			Tag.persist(tag);
		}
	}
	
	public static void parse_option(Document document) throws ParseException{
		String title = Xml.getText(document, "//rss/channel/title");
		Option.persist("blog_name", title);
		
		String description = Xml.getText(document, "//rss/channel/description");
		Option.persist("blog_description", title);
		
		String pubDate = Xml.getText(document, "//rss/channel/pubDate");
		/** Sat, 14 May 2011 05:41:30 +0000*/
		SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss '+0000'", Locale.ENGLISH);
		Option.persist("blog_pubDate", dateFormat.parse(pubDate));
		
		User user = new User();
		user.name = Xml.getText(document, "//rss/channel/wp:author/wp:author_login");
		user.displayName =  Xml.getText(document, "//rss/channel/wp:author/wp:author_display_name");
		user.firstName = Xml.getText(document, "//rss/channel/wp:author/wp:author_first_name");
		user.lastName = Xml.getText(document, "//rss/channel/wp:author/wp:author_last_name");
		user.email =  Xml.getText(document, "//rss/channel/wp:author/wp:author_email");
		User.persist(user);
	}
	
	public static void parse_category(Document document){
		for(Node item : document.selectNodes("//rss/channel/wp:category")){
			String cat_name = Xml.getText(item, "wp:cat_name");
			Category cat = Category.getByName(cat_name);
			if(cat == null){
				cat = new Category();
			}
			cat.name = cat_name;
			cat.slug = Xml.getText(item, "wp:category_nicename");
			String parentName = Xml.getText(item, "wp:category_parent");
			if(StringUtil.isNotBlank(parentName)){
				Category pa = Category.getByName(parentName);
				if(pa == null){
					pa = new Category();
					pa.name = parentName;
					Category.persist(pa);
				}
				cat.parentId = pa.id;
			}
			
			Category.persist(cat);
			
		}
		
	}
	
	public static void parse_post(Document document) throws ParseException{
		/*2010-10-30 12:18:05*/
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.ENGLISH);
		for(Node item : document.selectNodes("//rss/channel/item")){
			if(Xml.getText(item, "wp:post_type").equals("page")){
				Page page = new Page();
				page.title = Xml.getText(item, "title");
				page.name = Xml.getText(item, "wp:post_name");
				page.pubDate = dateFormat.parse(Xml.getText(item, "wp:post_date"));
				Page.persist(page);
				continue;
			}
			Article article = new Article();
			article.title = Xml.getText(item, "title");
			article.name = Xml.getText(item, "wp:post_name");
			article.content = Xml.getText(item, "content:encoded");
			article.pubDate = dateFormat.parse(Xml.getText(item, "wp:post_date"));
			Article.persist(article);
			for(Node node : item.selectNodes("category[@domain='category']")){
				Category category = Category.getByName(Xml.getText(node));
				if(category != null){
					article.addCategory(category);
				}
			}
			for(Node node : item.selectNodes("category[@domain='post_tag']")){
				Tag tag = Tag.getByName(node.getText());
				if(tag != null){
					article.addTag(tag);
				}
			}
		}
		
	}
}
